<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveModel::Validations::HelperMethods</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../css/github.css" type="text/css" media="screen" />
<script src="../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveModel::Validations::HelperMethods 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../../files/activemodel/lib/active_model/validations/absence_rb.html">activemodel/lib/active_model/validations/absence.rb</a></li>
            
            <li><a href="../../../files/activemodel/lib/active_model/validations/acceptance_rb.html">activemodel/lib/active_model/validations/acceptance.rb</a></li>
            
            <li><a href="../../../files/activemodel/lib/active_model/validations/confirmation_rb.html">activemodel/lib/active_model/validations/confirmation.rb</a></li>
            
            <li><a href="../../../files/activemodel/lib/active_model/validations/exclusion_rb.html">activemodel/lib/active_model/validations/exclusion.rb</a></li>
            
            <li><a href="../../../files/activemodel/lib/active_model/validations/format_rb.html">activemodel/lib/active_model/validations/format.rb</a></li>
            
            <li><a href="../../../files/activemodel/lib/active_model/validations/inclusion_rb.html">activemodel/lib/active_model/validations/inclusion.rb</a></li>
            
            <li><a href="../../../files/activemodel/lib/active_model/validations/length_rb.html">activemodel/lib/active_model/validations/length.rb</a></li>
            
            <li><a href="../../../files/activemodel/lib/active_model/validations/numericality_rb.html">activemodel/lib/active_model/validations/numericality.rb</a></li>
            
            <li><a href="../../../files/activemodel/lib/active_model/validations/presence_rb.html">activemodel/lib/active_model/validations/presence.rb</a></li>
            
            <li><a href="../../../files/activemodel/lib/active_model/validations/with_rb.html">activemodel/lib/active_model/validations/with.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>V</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="HelperMethods.html#method-i-validates_absence_of">validates_absence_of</a>,
              </li>
            
              
              <li>
                <a href="HelperMethods.html#method-i-validates_acceptance_of">validates_acceptance_of</a>,
              </li>
            
              
              <li>
                <a href="HelperMethods.html#method-i-validates_confirmation_of">validates_confirmation_of</a>,
              </li>
            
              
              <li>
                <a href="HelperMethods.html#method-i-validates_exclusion_of">validates_exclusion_of</a>,
              </li>
            
              
              <li>
                <a href="HelperMethods.html#method-i-validates_format_of">validates_format_of</a>,
              </li>
            
              
              <li>
                <a href="HelperMethods.html#method-i-validates_inclusion_of">validates_inclusion_of</a>,
              </li>
            
              
              <li>
                <a href="HelperMethods.html#method-i-validates_length_of">validates_length_of</a>,
              </li>
            
              
              <li>
                <a href="HelperMethods.html#method-i-validates_numericality_of">validates_numericality_of</a>,
              </li>
            
              
              <li>
                <a href="HelperMethods.html#method-i-validates_presence_of">validates_presence_of</a>,
              </li>
            
              
              <li>
                <a href="HelperMethods.html#method-i-validates_size_of">validates_size_of</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-validates_absence_of">
            
              <b>validates_absence_of</b>(*attr_names)
            
            <a href="HelperMethods.html#method-i-validates_absence_of" name="method-i-validates_absence_of" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Validates that the specified attributes are blank (as defined by <a
href="../../Object.html#method-i-blank-3F">Object#blank?</a>). Happens by
default on save.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">validates_absence_of</span> :<span class="ruby-identifier">first_name</span>
<span class="ruby-keyword">end</span>
</pre>

<p>The first_name attribute must be in the object and it must be blank.</p>

<p>Configuration options:</p>
<ul><li>
<p><code>:message</code> - A custom error message (default is: "must be
blank").</p>
</li></ul>

<p>There is also a list of default options supported by every validator:
<code>:if</code>, <code>:unless</code>, <code>:on</code> and
<code>:strict</code>. See <code>ActiveModel::Validation#validates</code>
for more information</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-validates_absence_of_source')" id="l_method-i-validates_absence_of_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/ac6941fb714b17a5fab5de8e9265e426feb0fad9/activemodel/lib/active_model/validations/absence.rb#L26" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-validates_absence_of_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validations/absence.rb, line 26</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">validates_absence_of</span>(*<span class="ruby-identifier">attr_names</span>)
  <span class="ruby-identifier">validates_with</span> <span class="ruby-constant">AbsenceValidator</span>, <span class="ruby-identifier">_merge_attributes</span>(<span class="ruby-identifier">attr_names</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-validates_acceptance_of">
            
              <b>validates_acceptance_of</b>(*attr_names)
            
            <a href="HelperMethods.html#method-i-validates_acceptance_of" name="method-i-validates_acceptance_of" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Encapsulates the pattern of wanting to validate the acceptance of a terms
of service check box (or similar agreement).</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">validates_acceptance_of</span> :<span class="ruby-identifier">terms_of_service</span>
  <span class="ruby-identifier">validates_acceptance_of</span> :<span class="ruby-identifier">eula</span>, <span class="ruby-identifier">message</span><span class="ruby-operator">:</span> <span class="ruby-string">'must be abided'</span>
<span class="ruby-keyword">end</span>
</pre>

<p>If the database column does not exist, the <code>terms_of_service</code>
attribute is entirely virtual. This check is performed only if
<code>terms_of_service</code> is not <code>nil</code> and by default on
save.</p>

<p>Configuration options:</p>
<ul><li>
<p><code>:message</code> - A custom error message (default is: "must be
accepted").</p>
</li><li>
<p><code>:allow_nil</code> - Skip validation if attribute is <code>nil</code>
(default is <code>true</code>).</p>
</li><li>
<p><code>:accept</code> - Specifies value that is considered accepted. The
default value is a string "1", which makes it easy to relate to an <a
href="../../HTML.html">HTML</a> checkbox. This should be set to
<code>true</code> if you are validating a database column, since the
attribute is typecast from "1" to <code>true</code> before validation.</p>
</li></ul>

<p>There is also a list of default options supported by every validator:
<code>:if</code>, <code>:unless</code>, <code>:on</code> and
<code>:strict</code>. See <code>ActiveModel::Validation#validates</code>
for more information</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-validates_acceptance_of_source')" id="l_method-i-validates_acceptance_of_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/9ac095fef586bcf86ed10d31acbc4af2abb53843/activemodel/lib/active_model/validations/acceptance.rb#L50" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-validates_acceptance_of_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validations/acceptance.rb, line 50</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">validates_acceptance_of</span>(*<span class="ruby-identifier">attr_names</span>)
  <span class="ruby-identifier">validates_with</span> <span class="ruby-constant">AcceptanceValidator</span>, <span class="ruby-identifier">_merge_attributes</span>(<span class="ruby-identifier">attr_names</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-validates_confirmation_of">
            
              <b>validates_confirmation_of</b>(*attr_names)
            
            <a href="HelperMethods.html#method-i-validates_confirmation_of" name="method-i-validates_confirmation_of" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Encapsulates the pattern of wanting to validate a password or email address
field with a confirmation.</p>

<pre>Model:
  class Person &lt; ActiveRecord::Base
    validates_confirmation_of :user_name, :password
    validates_confirmation_of :email_address,
                              message: 'should match confirmation'
  end

View:
  &lt;%= password_field &quot;person&quot;, &quot;password&quot; %&gt;
  &lt;%= password_field &quot;person&quot;, &quot;password_confirmation&quot; %&gt;</pre>

<p>The added <code>password_confirmation</code> attribute is virtual; it
exists only as an in-memory attribute for validating the password. To
achieve this, the validation adds accessors to the model for the
confirmation attribute.</p>

<p>NOTE: This check is performed only if <code>password_confirmation</code> is
not <code>nil</code>. To require confirmation, make sure to add a presence
check for the confirmation attribute:</p>

<pre>validates_presence_of :password_confirmation, if: :password_changed?</pre>

<p>Configuration options:</p>
<ul><li>
<p><code>:message</code> - A custom error message (default is: "doesn't match
confirmation").</p>
</li></ul>

<p>There is also a list of default options supported by every validator:
<code>:if</code>, <code>:unless</code>, <code>:on</code> and
<code>:strict</code>. See <code>ActiveModel::Validation#validates</code>
for more information</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-validates_confirmation_of_source')" id="l_method-i-validates_confirmation_of_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/b501ee47fa3f877f8b8028e732f8ef8a22cc75fb/activemodel/lib/active_model/validations/confirmation.rb#L56" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-validates_confirmation_of_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validations/confirmation.rb, line 56</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">validates_confirmation_of</span>(*<span class="ruby-identifier">attr_names</span>)
  <span class="ruby-identifier">validates_with</span> <span class="ruby-constant">ConfirmationValidator</span>, <span class="ruby-identifier">_merge_attributes</span>(<span class="ruby-identifier">attr_names</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-validates_exclusion_of">
            
              <b>validates_exclusion_of</b>(*attr_names)
            
            <a href="HelperMethods.html#method-i-validates_exclusion_of" name="method-i-validates_exclusion_of" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Validates that the value of the specified attribute is not in a particular
enumerable object.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">validates_exclusion_of</span> :<span class="ruby-identifier">username</span>, <span class="ruby-keyword">in</span><span class="ruby-operator">:</span> <span class="ruby-node">%w( admin superuser )</span>, <span class="ruby-identifier">message</span><span class="ruby-operator">:</span> <span class="ruby-string">&quot;You don't belong here&quot;</span>
  <span class="ruby-identifier">validates_exclusion_of</span> :<span class="ruby-identifier">age</span>, <span class="ruby-keyword">in</span><span class="ruby-operator">:</span> <span class="ruby-value">30</span><span class="ruby-operator">..</span><span class="ruby-value">60</span>, <span class="ruby-identifier">message</span><span class="ruby-operator">:</span> <span class="ruby-string">'This site is only for under 30 and over 60'</span>
  <span class="ruby-identifier">validates_exclusion_of</span> :<span class="ruby-identifier">format</span>, <span class="ruby-keyword">in</span><span class="ruby-operator">:</span> <span class="ruby-node">%w( mov avi )</span>, <span class="ruby-identifier">message</span><span class="ruby-operator">:</span> <span class="ruby-string">&quot;extension %{value} is not allowed&quot;</span>
  <span class="ruby-identifier">validates_exclusion_of</span> :<span class="ruby-identifier">password</span>, <span class="ruby-keyword">in</span><span class="ruby-operator">:</span> <span class="ruby-operator">-</span><span class="ruby-operator">&gt;</span>(<span class="ruby-identifier">person</span>) { [<span class="ruby-identifier">person</span>.<span class="ruby-identifier">username</span>, <span class="ruby-identifier">person</span>.<span class="ruby-identifier">first_name</span>] },
                         <span class="ruby-identifier">message</span><span class="ruby-operator">:</span> <span class="ruby-string">'should not be the same as your username or first name'</span>
  <span class="ruby-identifier">validates_exclusion_of</span> :<span class="ruby-identifier">karma</span>, <span class="ruby-keyword">in</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">reserved_karmas</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Configuration options:</p>
<ul><li>
<p><code>:in</code> - An enumerable object of items that the value shouldn't
be part of. This can be supplied as a proc, lambda or symbol which returns
an enumerable. If the enumerable is a range the test is performed with</p>
</li><li>
<p><code>:within</code> - A synonym(or alias) for <code>:in</code>
<code>Range#cover?</code>, otherwise with <code>include?</code>.</p>
</li><li>
<p><code>:message</code> - Specifies a custom error message (default is: "is
reserved").</p>
</li><li>
<p><code>:allow_nil</code> - If set to true, skips this validation if the
attribute is <code>nil</code> (default is <code>false</code>).</p>
</li><li>
<p><code>:allow_blank</code> - If set to true, skips this validation if the
attribute is blank(default is <code>false</code>).</p>
</li></ul>

<p>There is also a list of default options supported by every validator:
<code>:if</code>, <code>:unless</code>, <code>:on</code> and
<code>:strict</code>. See <code>ActiveModel::Validation#validates</code>
for more information</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-validates_exclusion_of_source')" id="l_method-i-validates_exclusion_of_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/9ac095fef586bcf86ed10d31acbc4af2abb53843/activemodel/lib/active_model/validations/exclusion.rb#L45" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-validates_exclusion_of_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validations/exclusion.rb, line 45</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">validates_exclusion_of</span>(*<span class="ruby-identifier">attr_names</span>)
  <span class="ruby-identifier">validates_with</span> <span class="ruby-constant">ExclusionValidator</span>, <span class="ruby-identifier">_merge_attributes</span>(<span class="ruby-identifier">attr_names</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-validates_format_of">
            
              <b>validates_format_of</b>(*attr_names)
            
            <a href="HelperMethods.html#method-i-validates_format_of" name="method-i-validates_format_of" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Validates whether the value of the specified attribute is of the correct
form, going by the regular expression provided.You can require that the
attribute matches the regular expression:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">validates_format_of</span> :<span class="ruby-identifier">email</span>, <span class="ruby-identifier">with</span><span class="ruby-operator">:</span> <span class="ruby-regexp">/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/</span>, <span class="ruby-identifier">on</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">create</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Alternatively, you can require that the specified attribute does
<em>not</em> match the regular expression:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">validates_format_of</span> :<span class="ruby-identifier">email</span>, <span class="ruby-identifier">without</span><span class="ruby-operator">:</span> <span class="ruby-regexp">/NOSPAM/</span>
<span class="ruby-keyword">end</span>
</pre>

<p>You can also provide a proc or lambda which will determine the regular
expression that will be used to validate the attribute.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-comment"># Admin can have number as a first letter in their screen name</span>
  <span class="ruby-identifier">validates_format_of</span> :<span class="ruby-identifier">screen_name</span>,
                      <span class="ruby-identifier">with</span><span class="ruby-operator">:</span> <span class="ruby-operator">-</span><span class="ruby-operator">&gt;</span>(<span class="ruby-identifier">person</span>) { <span class="ruby-identifier">person</span>.<span class="ruby-identifier">admin?</span> <span class="ruby-operator">?</span> <span class="ruby-regexp">/\A[a-z0-9][a-z0-9_\-]*\z/</span> <span class="ruby-operator">:</span> <span class="ruby-regexp">/\A[a-z][a-z0-9_\-]*\z/</span> }
<span class="ruby-keyword">end</span>
</pre>

<p>Note: use <code>\A</code> and <code>\Z</code> to match the start and end of
the string, <code>^</code> and <code>$</code> match the start/end of a
line.</p>

<p>Due to frequent misuse of <code>^</code> and <code>$</code>, you need to
pass the <code>multiline: true</code> option in case you use any of these
two anchors in the provided regular expression. In most cases, you should
be using <code>\A</code> and <code>\z</code>.</p>

<p>You must pass either <code>:with</code> or <code>:without</code> as an
option. In addition, both must be a regular expression or a proc or lambda,
or else an exception will be raised.</p>

<p>Configuration options:</p>
<ul><li>
<p><code>:message</code> - A custom error message (default is: "is invalid").</p>
</li><li>
<p><code>:allow_nil</code> - If set to true, skips this validation if the
attribute is <code>nil</code> (default is <code>false</code>).</p>
</li><li>
<p><code>:allow_blank</code> - If set to true, skips this validation if the
attribute is blank (default is <code>false</code>).</p>
</li><li>
<p><code>:with</code> - Regular expression that if the attribute matches will
result in a successful validation. This can be provided as a proc or lambda
returning regular expression which will be called at runtime.</p>
</li><li>
<p><code>:without</code> - Regular expression that if the attribute does not
match will result in a successful validation. This can be provided as a
proc or lambda returning regular expression which will be called at
runtime.</p>
</li><li>
<p><code>:multiline</code> - Set to true if your regular expression contains
anchors that match the beginning or end of lines as opposed to the
beginning or end of the string. These anchors are <code>^</code> and
<code>$</code>.</p>
</li></ul>

<p>There is also a list of default options supported by every validator:
<code>:if</code>, <code>:unless</code>, <code>:on</code> and
<code>:strict</code>. See <code>ActiveModel::Validation#validates</code>
for more information</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-validates_format_of_source')" id="l_method-i-validates_format_of_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/9ac095fef586bcf86ed10d31acbc4af2abb53843/activemodel/lib/active_model/validations/format.rb#L110" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-validates_format_of_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validations/format.rb, line 110</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">validates_format_of</span>(*<span class="ruby-identifier">attr_names</span>)
  <span class="ruby-identifier">validates_with</span> <span class="ruby-constant">FormatValidator</span>, <span class="ruby-identifier">_merge_attributes</span>(<span class="ruby-identifier">attr_names</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-validates_inclusion_of">
            
              <b>validates_inclusion_of</b>(*attr_names)
            
            <a href="HelperMethods.html#method-i-validates_inclusion_of" name="method-i-validates_inclusion_of" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Validates whether the value of the specified attribute is available in a
particular enumerable object.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">validates_inclusion_of</span> :<span class="ruby-identifier">gender</span>, <span class="ruby-keyword">in</span><span class="ruby-operator">:</span> <span class="ruby-node">%w( m f )</span>
  <span class="ruby-identifier">validates_inclusion_of</span> :<span class="ruby-identifier">age</span>, <span class="ruby-keyword">in</span><span class="ruby-operator">:</span> <span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">99</span>
  <span class="ruby-identifier">validates_inclusion_of</span> :<span class="ruby-identifier">format</span>, <span class="ruby-keyword">in</span><span class="ruby-operator">:</span> <span class="ruby-node">%w( jpg gif png )</span>, <span class="ruby-identifier">message</span><span class="ruby-operator">:</span> <span class="ruby-string">&quot;extension %{value} is not included in the list&quot;</span>
  <span class="ruby-identifier">validates_inclusion_of</span> :<span class="ruby-identifier">states</span>, <span class="ruby-keyword">in</span><span class="ruby-operator">:</span> <span class="ruby-operator">-</span><span class="ruby-operator">&gt;</span>(<span class="ruby-identifier">person</span>) { <span class="ruby-constant">STATES</span>[<span class="ruby-identifier">person</span>.<span class="ruby-identifier">country</span>] }
  <span class="ruby-identifier">validates_inclusion_of</span> :<span class="ruby-identifier">karma</span>, <span class="ruby-keyword">in</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">available_karmas</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Configuration options:</p>
<ul><li>
<p><code>:in</code> - An enumerable object of available items. This can be
supplied as a proc, lambda or symbol which returns an enumerable. If the
enumerable is a range the test is performed with <code>Range#cover?</code>,
otherwise with <code>include?</code>.</p>
</li><li>
<p><code>:within</code> - A synonym(or alias) for <code>:in</code></p>
</li><li>
<p><code>:message</code> - Specifies a custom error message (default is: "is
not included in the list").</p>
</li><li>
<p><code>:allow_nil</code> - If set to <code>true</code>, skips this
validation if the attribute is <code>nil</code> (default is
<code>false</code>).</p>
</li><li>
<p><code>:allow_blank</code> - If set to <code>true</code>, skips this
validation if the attribute is blank (default is <code>false</code>).</p>
</li></ul>

<p>There is also a list of default options supported by every validator:
<code>:if</code>, <code>:unless</code>, <code>:on</code> and
<code>:strict</code>. See <code>ActiveModel::Validation#validates</code>
for more information</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-validates_inclusion_of_source')" id="l_method-i-validates_inclusion_of_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/9ac095fef586bcf86ed10d31acbc4af2abb53843/activemodel/lib/active_model/validations/inclusion.rb#L44" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-validates_inclusion_of_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validations/inclusion.rb, line 44</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">validates_inclusion_of</span>(*<span class="ruby-identifier">attr_names</span>)
  <span class="ruby-identifier">validates_with</span> <span class="ruby-constant">InclusionValidator</span>, <span class="ruby-identifier">_merge_attributes</span>(<span class="ruby-identifier">attr_names</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-validates_length_of">
            
              <b>validates_length_of</b>(*attr_names)
            
            <a href="HelperMethods.html#method-i-validates_length_of" name="method-i-validates_length_of" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Validates that the specified attribute matches the length restrictions
supplied. Only one option can be used at a time:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">validates_length_of</span> :<span class="ruby-identifier">first_name</span>, <span class="ruby-identifier">maximum</span><span class="ruby-operator">:</span> <span class="ruby-value">30</span>
  <span class="ruby-identifier">validates_length_of</span> :<span class="ruby-identifier">last_name</span>, <span class="ruby-identifier">maximum</span><span class="ruby-operator">:</span> <span class="ruby-value">30</span>, <span class="ruby-identifier">message</span><span class="ruby-operator">:</span> <span class="ruby-string">&quot;less than 30 if you don't mind&quot;</span>
  <span class="ruby-identifier">validates_length_of</span> :<span class="ruby-identifier">fax</span>, <span class="ruby-keyword">in</span><span class="ruby-operator">:</span> <span class="ruby-value">7</span><span class="ruby-operator">..</span><span class="ruby-value">32</span>, <span class="ruby-identifier">allow_nil</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>
  <span class="ruby-identifier">validates_length_of</span> :<span class="ruby-identifier">phone</span>, <span class="ruby-keyword">in</span><span class="ruby-operator">:</span> <span class="ruby-value">7</span><span class="ruby-operator">..</span><span class="ruby-value">32</span>, <span class="ruby-identifier">allow_blank</span><span class="ruby-operator">:</span> <span class="ruby-keyword">true</span>
  <span class="ruby-identifier">validates_length_of</span> :<span class="ruby-identifier">user_name</span>, <span class="ruby-identifier">within</span><span class="ruby-operator">:</span> <span class="ruby-value">6</span><span class="ruby-operator">..</span><span class="ruby-value">20</span>, <span class="ruby-identifier">too_long</span><span class="ruby-operator">:</span> <span class="ruby-string">'pick a shorter name'</span>, <span class="ruby-identifier">too_short</span><span class="ruby-operator">:</span> <span class="ruby-string">'pick a longer name'</span>
  <span class="ruby-identifier">validates_length_of</span> :<span class="ruby-identifier">zip_code</span>, <span class="ruby-identifier">minimum</span><span class="ruby-operator">:</span> <span class="ruby-value">5</span>, <span class="ruby-identifier">too_short</span><span class="ruby-operator">:</span> <span class="ruby-string">'please enter at least 5 characters'</span>
  <span class="ruby-identifier">validates_length_of</span> :<span class="ruby-identifier">smurf_leader</span>, <span class="ruby-identifier">is</span><span class="ruby-operator">:</span> <span class="ruby-value">4</span>, <span class="ruby-identifier">message</span><span class="ruby-operator">:</span> <span class="ruby-string">&quot;papa is spelled with 4 characters... don't play me.&quot;</span>
  <span class="ruby-identifier">validates_length_of</span> :<span class="ruby-identifier">essay</span>, <span class="ruby-identifier">minimum</span><span class="ruby-operator">:</span> <span class="ruby-value">100</span>, <span class="ruby-identifier">too_short</span><span class="ruby-operator">:</span> <span class="ruby-string">'Your essay must be at least 100 words.'</span>,
                      <span class="ruby-identifier">tokenizer</span><span class="ruby-operator">:</span> <span class="ruby-operator">-</span><span class="ruby-operator">&gt;</span>(<span class="ruby-identifier">str</span>) { <span class="ruby-identifier">str</span>.<span class="ruby-identifier">scan</span>(<span class="ruby-regexp">/\w+/</span>) }
<span class="ruby-keyword">end</span>
</pre>

<p>Configuration options:</p>
<ul><li>
<p><code>:minimum</code> - The minimum size of the attribute.</p>
</li><li>
<p><code>:maximum</code> - The maximum size of the attribute. Allows
<code>nil</code> by default if not used with :minimum.</p>
</li><li>
<p><code>:is</code> - The exact size of the attribute.</p>
</li><li>
<p><code>:within</code> - A range specifying the minimum and maximum size of
the attribute.</p>
</li><li>
<p><code>:in</code> - A synonym (or alias) for <code>:within</code>.</p>
</li><li>
<p><code>:allow_nil</code> - Attribute may be <code>nil</code>; skip
validation.</p>
</li><li>
<p><code>:allow_blank</code> - Attribute may be blank; skip validation.</p>
</li><li>
<p><code>:too_long</code> - The error message if the attribute goes over the
maximum (default is: "is too long (maximum is %{count} characters)").</p>
</li><li>
<p><code>:too_short</code> - The error message if the attribute goes under the
minimum (default is: "is too short (min is %{count} characters)").</p>
</li><li>
<p><code>:wrong_length</code> - The error message if using the
<code>:is</code> method and the attribute is the wrong size (default is:
"is the wrong length (should be %{count} characters)").</p>
</li><li>
<p><code>:message</code> - The error message to use for a
<code>:minimum</code>, <code>:maximum</code>, or <code>:is</code>
violation. An alias of the appropriate
<code>too_long</code>/<code>too_short</code>/<code>wrong_length</code>
message.</p>
</li><li>
<p><code>:tokenizer</code> - Specifies how to split up the attribute string.
(e.g. <code>tokenizer: -&gt;(str) { str.scan(/\w+/) }</code> to count words
as in above example). Defaults to <code>-&gt;(value) { value.split(//)
}</code> which counts individual characters.</p>
</li></ul>

<p>There is also a list of default options supported by every validator:
<code>:if</code>, <code>:unless</code>, <code>:on</code> and
<code>:strict</code>. See <code>ActiveModel::Validation#validates</code>
for more information</p>
            </div>
          
          
          
            <div class="aka">
              Also aliased as: <a href="HelperMethods.html#method-i-validates_size_of">validates_size_of</a>
            </div>
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-validates_length_of_source')" id="l_method-i-validates_length_of_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/ea76e9a3126998578d683783483aa695cb6b657e/activemodel/lib/active_model/validations/length.rb#L119" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-validates_length_of_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validations/length.rb, line 119</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">validates_length_of</span>(*<span class="ruby-identifier">attr_names</span>)
  <span class="ruby-identifier">validates_with</span> <span class="ruby-constant">LengthValidator</span>, <span class="ruby-identifier">_merge_attributes</span>(<span class="ruby-identifier">attr_names</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-validates_numericality_of">
            
              <b>validates_numericality_of</b>(*attr_names)
            
            <a href="HelperMethods.html#method-i-validates_numericality_of" name="method-i-validates_numericality_of" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Validates whether the value of the specified attribute is numeric by trying
to convert it to a float with <a href="../../Kernel.html">Kernel</a>.<a
href="../../Float.html">Float</a> (if <code>only_integer</code> is
<code>false</code>) or applying it to the regular expression
<code>/\A[+\-]?\d+\Z/</code> (if <code>only_integer</code> is set to
<code>true</code>).</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">validates_numericality_of</span> :<span class="ruby-identifier">value</span>, <span class="ruby-identifier">on</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">create</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Configuration options:</p>
<ul><li>
<p><code>:message</code> - A custom error message (default is: "is not a
number").</p>
</li><li>
<p><code>:only_integer</code> - Specifies whether the value has to be an
integer, e.g. an integral value (default is <code>false</code>).</p>
</li><li>
<p><code>:allow_nil</code> - Skip validation if attribute is <code>nil</code>
(default is <code>false</code>). Notice that for fixnum and float columns
empty strings are converted to <code>nil</code>.</p>
</li><li>
<p><code>:greater_than</code> - Specifies the value must be greater than the
supplied value.</p>
</li><li>
<p><code>:greater_than_or_equal_to</code> - Specifies the value must be
greater than or equal the supplied value.</p>
</li><li>
<p><code>:equal_to</code> - Specifies the value must be equal to the supplied
value.</p>
</li><li>
<p><code>:less_than</code> - Specifies the value must be less than the
supplied value.</p>
</li><li>
<p><code>:less_than_or_equal_to</code> - Specifies the value must be less than
or equal the supplied value.</p>
</li><li>
<p><code>:other_than</code> - Specifies the value must be other than the
supplied value.</p>
</li><li>
<p><code>:odd</code> - Specifies the value must be an odd number.</p>
</li><li>
<p><code>:even</code> - Specifies the value must be an even number.</p>
</li></ul>

<p>There is also a list of default options supported by every validator:
<code>:if</code>, <code>:unless</code>, <code>:on</code> and
<code>:strict</code> . See <code>ActiveModel::Validation#validates</code>
for more information</p>

<p>The following checks can also be supplied with a proc or a symbol which
corresponds to a method:</p>
<ul><li>
<p><code>:greater_than</code></p>
</li><li>
<p><code>:greater_than_or_equal_to</code></p>
</li><li>
<p><code>:equal_to</code></p>
</li><li>
<p><code>:less_than</code></p>
</li><li>
<p><code>:less_than_or_equal_to</code></p>
</li></ul>

<p>For example:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">validates_numericality_of</span> :<span class="ruby-identifier">width</span>, <span class="ruby-identifier">less_than</span><span class="ruby-operator">:</span> <span class="ruby-operator">-</span><span class="ruby-operator">&gt;</span>(<span class="ruby-identifier">person</span>) { <span class="ruby-identifier">person</span>.<span class="ruby-identifier">height</span> }
  <span class="ruby-identifier">validates_numericality_of</span> :<span class="ruby-identifier">width</span>, <span class="ruby-identifier">greater_than</span><span class="ruby-operator">:</span> :<span class="ruby-identifier">minimum_weight</span>
<span class="ruby-keyword">end</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-validates_numericality_of_source')" id="l_method-i-validates_numericality_of_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/3c21237c37fed9d726bcf2816bf1adc4635f4956/activemodel/lib/active_model/validations/numericality.rb#L131" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-validates_numericality_of_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validations/numericality.rb, line 131</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">validates_numericality_of</span>(*<span class="ruby-identifier">attr_names</span>)
  <span class="ruby-identifier">validates_with</span> <span class="ruby-constant">NumericalityValidator</span>, <span class="ruby-identifier">_merge_attributes</span>(<span class="ruby-identifier">attr_names</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-validates_presence_of">
            
              <b>validates_presence_of</b>(*attr_names)
            
            <a href="HelperMethods.html#method-i-validates_presence_of" name="method-i-validates_presence_of" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Validates that the specified attributes are not blank (as defined by <a
href="../../Object.html#method-i-blank-3F">Object#blank?</a>). Happens by
default on save.</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">validates_presence_of</span> :<span class="ruby-identifier">first_name</span>
<span class="ruby-keyword">end</span>
</pre>

<p>The first_name attribute must be in the object and it cannot be blank.</p>

<p>If you want to validate the presence of a boolean field (where the real
values are <code>true</code> and <code>false</code>), you will want to use
<code>validates_inclusion_of :field_name, in: [true, false]</code>.</p>

<p>This is due to the way <a
href="../../Object.html#method-i-blank-3F">Object#blank?</a> handles
boolean values: <code>false.blank? # =&gt; true</code>.</p>

<p>Configuration options:</p>
<ul><li>
<p><code>:message</code> - A custom error message (default is: "can't be
blank").</p>
</li></ul>

<p>There is also a list of default options supported by every validator:
<code>:if</code>, <code>:unless</code>, <code>:on</code> and
<code>:strict</code>. See <code>ActiveModel::Validation#validates</code>
for more information</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-validates_presence_of_source')" id="l_method-i-validates_presence_of_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/78fd14c8de9577f92ee78ba399ca716589465465/activemodel/lib/active_model/validations/presence.rb#L34" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-validates_presence_of_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/validations/presence.rb, line 34</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">validates_presence_of</span>(*<span class="ruby-identifier">attr_names</span>)
  <span class="ruby-identifier">validates_with</span> <span class="ruby-constant">PresenceValidator</span>, <span class="ruby-identifier">_merge_attributes</span>(<span class="ruby-identifier">attr_names</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-validates_size_of">
            
              <b>validates_size_of</b>(*attr_names)
            
            <a href="HelperMethods.html#method-i-validates_size_of" name="method-i-validates_size_of" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              
            </div>
          
          
          
          
          
          </div>
                    </div>

    </div>
  </body>
</html>    